package ink.lovejinhu.leetcode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-07-12 9:36
 */
public class Solution_0290 {
    /**
     * 单词规律
     *
     * @param pattern
     * @param s
     * @return
     */
    public boolean wordPattern(String pattern, String s) {
        char[] chars = pattern.toCharArray();
        String[] patterned = s.split(" ");
        if (patterned.length != chars.length) return false;
        HashMap<Character, Integer> charsMap = new HashMap<>();
        HashMap<String, Integer> patternedMap = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (charsMap.containsKey(chars[i])) {  //包含
                if (patternedMap.containsKey(patterned[i])) { //包含
                    Integer v0 = charsMap.get(chars[i]);
                    Integer v1 = patternedMap.get(patterned[i]);
                    if (v0 == v1) {
                        charsMap.put(chars[i], i);
                        patternedMap.put(patterned[i], i);
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                if (patternedMap.containsKey(patterned[i])) {
                    return false;
                } else {
                    charsMap.put(chars[i], i);
                    patternedMap.put(patterned[i], i);
                }

            }
        }

        return true;
    }

    public static void main(String[] args) {


        String pattern = "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdd";
        String s = "s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s s t t";
        Solution_0290 solution_0290 = new Solution_0290();
        solution_0290.wordPattern(pattern, s);
        System.out.println(solution_0290.wordPattern(pattern, s));
        pattern = "aaabb";
        s = "s s s d d";
        System.out.println(solution_0290.wordPattern(pattern, s));

    }
}
